#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <cbm.h>
#include <conio.h>

#include "Buffers.h"
#include "Trim.h"
#include "Splitter.h"
#include "Loader.h"

#pragma code-name("HELPCODE");
#pragma rodata-name("HELPCODE");

unsigned char helpToken(tokenCollection_t *collection, char *token, char *file)
{
	if(strcmp(collection->tokens[1].text, token) == 0 || strlen(token) == 0)
	{
		if(eapi_load(file) == 1)
		{
			printf("\n%s", (void*)0x4000);
			return 0;
		}
		else
		{
			printf("\nCould not load help for %s.", token);
			return 1;
		}
	}
	return 0xff;
}

unsigned char help(unsigned char driveIndex)
{
	unsigned char r = 1;
	tokenCollection_t *collection = NULL;
	static char buffer[256];

	if(0) cgetc();

	strcpy(buffer, buffer3);
	collection = splitString(buffer);

	if(collection->count != 2)
	{
		return helpToken(collection, "", "HELP.HELP");
	}
	else
	{
		r = helpToken(collection, "cd", "HELP.CD");
		if(r == 0xff) r = helpToken(collection, "dir", "HELP.DIR");
		if(r == 0xff) r = helpToken(collection, "mkdir", "HELP.MKDIR");
		if(r == 0xff) r = helpToken(collection, "rmdir", "HELP.RMDIR");
		if(r == 0xff) r = helpToken(collection, "path", "HELP.PATH");
		if(r == 0xff) r = helpToken(collection, "del", "HELP.DEL");
		if(r == 0xff) r = helpToken(collection, "ren", "HELP.REN");
		if(r == 0xff) r = helpToken(collection, "copy", "HELP.COPY");
		if(r == 0xff) r = helpToken(collection, "move", "HELP.MOVE");
		if(r == 0xff) r = helpToken(collection, "show", "HELP.SHOW");
		if(r == 0xff) r = helpToken(collection, "mkimg", "HELP.MKIMG");
		if(r == 0xff) r = helpToken(collection, "wrimg", "HELP.WRIMG");
		if(r == 0xff) r = helpToken(collection, "", "HELP.HELP");
	}

	return r;
}
